<?php

/**
 * 会员推荐关系图
 * * @网店运维 (c) 2015-2018 ShopWWI Inc. (http://www.shopwwi.com)
 * @license    http://www.shopwwi.c om
 * @link       交流群号：111731672
 * @since      网店运维提供技术支持 授权请购买shopnc授权
 */

defined('ByShopWWI') or exit('Access Invalid!');

class member_networkControl extends BaseMemberControl {
     
     private static $downline_member_fee=0;//某会员的所有下线会员费总和
     private static $downline_members=array();//某会员的所有下线会员ID
   
     private static $member_array_list=[];
	public function __construct() {
            parent::__construct();
            Language::read('member_predeposit');

	}
      
	public function indexOp() {
             $this->networkOp();
             exit;
	}
	

	/**
	 * 会员推荐关系图
	 */
	public function networkOp() {

            if($this->member_info['member_type']!=5)
            {
                showDialog('无操作权限');
            }
            $inviter_id=!empty($_GET['inviter_id'])?(int)$_GET['inviter_id']:$_SESSION['member_id'];
            
            $this->getAllNodes($inviter_id);      
            $result= json_encode(self::$member_array_list);
    
            //信息输出
            self::profile_menu('log', 'loglist');
            Tpl::output('result', $result);
            Tpl::showpage('member_network');

	}

       
        
        
        
        
        
        
	public function getAllNodes($inviter_id)
        {
            $currentLevel=0;
            self::$member_array_list=[];
            $model_member=Model('member');
            $member= $model_member->where(array('member_id'=>$inviter_id))->find();
            if(!empty($member))
            {
               self::$member_array_list[0]['key']=$member['member_id'];
               self::$member_array_list[0]['parent']=$member['inviter_id'];
               self::$member_array_list[0]['member_name']=$member['member_name'].'('.$member['member_truename'].')(ID:'.$member['member_id'].')';
               self::$member_array_list[0]['is_active']=$member['member_state'];
               self::$member_array_list[0]['member_type']=$model_member->getMemberTypeName($member['member_type']);
               self::$downline_members=[];self::$downline_member_fee=0;
               $this->getDownLineMembersAndAchievement($member['member_id']);
               self::$member_array_list[0]['team_count']= count(self::$downline_members);
               self::$member_array_list[0]['profit']=self::$downline_member_fee;
               self::$member_array_list[0]['floor']='第【1】层';
               $currentLevel++;
                
            }
            $this->getChildNodes($inviter_id,$currentLevel);
        }
        
        
        public function getChildNodes($inviter_id,$currentLevel=0)
        {
          
            $model_member=Model('member');
            $member_array=array();
            $member_array_list=array();
            $member_list= $model_member->where(array('inviter_id'=>$inviter_id))->select();
            foreach($member_list as $member)
            {
               $member_array['key']=$member['member_id'];
               $member_array['parent']=$member['inviter_id'];
               $member_array['member_name']=$member['member_name'].'('.$member['member_truename'].')(ID:'.$member['member_id'].')';
               $member_array['is_active']=$member['member_state'];
               $member_array['member_type']=$model_member->getMemberTypeName($member['member_type']);
               self::$downline_members=[];self::$downline_member_fee=0;
               $this->getDownLineMembersAndAchievement($member['member_id']);
               $member_array['team_count']= count(self::$downline_members);
               $member_array['profit']=self::$downline_member_fee;
               $member_array['floor']='第【'.($currentLevel+1).'】层';
               self::$member_array_list[]=$member_array;
              
               $this->getChildNodes($member['member_id'],++$currentLevel);
               --$currentLevel;
            }
        }

        /**
         * 根据推荐人计算
         * 获取某会员的所有下线会员和此会员的总业绩
         * @param type $member_id
         */
        public function getDownLineMembersAndAchievement($member_id)
        {
            $model_member=Model('member');
            $member_fee_total=0;
            $childNodes=$model_member->getChildNodes($member_id);
            foreach($childNodes as $childNode)
            {   
                $member_fee=$childNode['member_fee'];
                $member_fee=$member_fee+$member_fee*$childNode['repeat_count'];
                $member_fee_total+=$member_fee;
                self::$downline_member_fee=self::$downline_member_fee+$member_fee_total;
                self::$downline_members[]=$childNode['member_id'];
                $this->getDownLineMembersAndAchievement($childNode['member_id']);
            }
        }
    
	
       
	
	/**
	 * 用户中心右边，小导航
	 *
	 * @param string    $menu_type  导航类型
	 * @param string    $menu_key   当前导航的menu_key
	 * @return
	 */

	private function profile_menu($menu_type, $menu_key = '') {

		$menu_array = array( array('menu_key' => 'loglist', 'menu_name' => '推荐关系图', 'menu_url' => 'index.php?act=member_network'));

		switch ($menu_type) {
			case 'log' :
			default :
				break;
		}

		Tpl::output('member_menu', $menu_array);
		Tpl::output('menu_key', $menu_key);

	}

}
